package com.zhulanhua.blogs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhulanhua.blogs.Utils.Code;
import com.zhulanhua.blogs.Utils.FileUtils;
import com.zhulanhua.blogs.Utils.PathUtils;
import com.zhulanhua.blogs.dao.BranchDao;
import com.zhulanhua.blogs.dao.ImageDao;
import com.zhulanhua.blogs.domain.Branch;
import com.zhulanhua.blogs.domain.Image;
import com.zhulanhua.blogs.domain.query.AlbumQuery;
import com.zhulanhua.blogs.exception.BusinessException;
import com.zhulanhua.blogs.service.AlbumsService;
import com.zhulanhua.blogs.service.ImagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class AlbumsServiceImpl implements AlbumsService {

    @Autowired
    BranchDao branchDao;

    @Autowired
    ImageDao imageDao;

    @Autowired
    ImagesService imagesService;

    @Autowired
    PathUtils pathUtils;

    @Override
    public List<Branch> getAlbums() {
        List<Branch> branches = branchDao.selectList(null);
        return branches;
    }

    @Override
    public Branch<Image> getAlbumsItem(long aid) {
        if (aid == 0){
            throw new BusinessException(Code.BUSINESS_ERR, "aid参数缺少");
        }
        Branch branch = branchDao.selectById(aid);
        if (branch == null){
            throw new BusinessException(Code.BUSINESS_ERR, "没有该相册");
        }
        QueryWrapper<Image> qw = new QueryWrapper<>();
        qw.lambda().eq(Image::getBranchId, aid);
        branch.setList(imageDao.selectList(qw));
        return branch;
    }

    @Override
    public Branch<Image> getQueryItemPage(AlbumQuery alb) {
        Page p = null;
        if (alb.getPage() == 0 && alb.getPageNum() == 0){
            p = new Page();
        }else {
            p = new Page(alb.getPage(), alb.getPageNum());
        }
        QueryWrapper<Image> qw = new QueryWrapper<>();
        qw.lambda().eq(Image::getBranchId, alb.getAid())
                   .lt(alb.getMinTime() != null, Image::getCreateTime, alb.getMinTime())
                   .gt(alb.getMaxTime() != null, Image::getCreateTime, alb.getMaxTime());

        Branch branch = branchDao.selectById(alb.getAid());
        if (branch == null){
            throw new BusinessException(Code.BUSINESS_ERR, "没有该相册");
        }
        imageDao.selectPage(p, qw);
        branch.setList(p.getRecords());
        return branch;
    }

    @Override
    public boolean saveAlbums(Branch branch) {
        if (branch.getBranchName().equals("") || branch.getBranchName() == null){
            throw new BusinessException(Code.BUSINESS_ERR, "相册名不能为空");
        }
        branch.setCreateTime(System.currentTimeMillis() + "");
        return branchDao.insert(branch) == 1;
    }

    @Override
    public boolean updateAlbums(Branch branch) {
        return branchDao.updateById(branch) > 0;
    }

    @Override
    public boolean deleteAlbums(long aid) {
        if (aid == 0){
            throw new BusinessException(Code.BUSINESS_ERR, "aid参数缺少");
        }
        QueryWrapper<Image> qw = new QueryWrapper<>();
        qw.lambda().eq(Image::getBranchId, aid);
        imageDao.delete(qw);
        return branchDao.deleteById(aid) > 0;
    }

    @Override
    public boolean deleteAlbumsItem(String imgs) {
        QueryWrapper<Image> qw = new QueryWrapper<>();
        qw.lambda().in(Image::getImageId, imgs.split(","));
        List<Image> images = imageDao.selectList(qw);
        for (Image  i : images){
            if (!FileUtils.deleteFile(pathUtils.path + i.getSrc())){
                throw new BusinessException(Code.BUSINESS_ERR, "删除照片失败FileErr");
            }
        }
        if (imageDao.delete(qw) != imgs.split(",").length){
            throw new BusinessException(Code.BUSINESS_ERR, "删除照片失败mysqlErr");
        }
        return true;
    }

    @Override
    public boolean saveAlbumsItem(int aid, String[] list) {
        int i = 0;
        for (String s : list){
            String src = imagesService.GenerateImage(s);
            src = src.replace("\\", "/");
            Image image = new Image();
            image.setBranchId(aid);
            image.setSrc(src);
            i += imageDao.insert(image);
        }
        return i == list.length;
    }
}
